package com.fzc.rhinooffice.module;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.fzc.rhinooffice.common.SysApplication;
import com.fzc.rhinooffice.common.utils.StringUtil;
import com.lidroid.xutils.util.LogUtils;

import java.util.HashMap;
import java.util.Map;

public class BaseActivity extends Activity {


    /**
     * =========  上下级activity的规则  =========*
     */
    private static Map<String, String> activityNameMap = new HashMap<String, String>() {
        {
            put("Detail", "List");
            put("List", "Home");
        }
    };
    private static String[] packName = new String[]{"office", "workbench"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SysApplication.addActivity(this);
    }




    /**
     * 要填写返回规则
     */
    public void back() {
        Class activityClass = null;
        String[] names = StringUtil.getWordsFromString(this.getClass().getSimpleName());


        int index = 0;
        while (index < packName.length) {
            if (names.length < 3) {
                activityClass = HomeActivity.class;
                break;
            }
            try {
                String secondName = activityNameMap.get(names[1]);
                String activityName = this.getPackageName() + ".module." + (index == 0 ? "" : packName[index - 1] + ".") + names[0] + secondName + names[2];
                activityClass = Class.forName(activityName);
                break;
            } catch (ClassNotFoundException e) {
                index++;
            }
        }

        if (activityClass == null) {
            activityClass = HomeActivity.class;
        }
        LogUtils.w("返回上一层：" + activityClass.getSimpleName());
        Intent intent = new Intent(this.getBaseContext(), activityClass);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK&& event.getAction() == KeyEvent.ACTION_DOWN) {
            back();
            return false;
        }
        return false;
    }




}
